package com.xxxx.mytest.jihe.setTest

import scala.collection.mutable.Set
/**
 * @program: day0303
 * @description: 尽信书，则不如无书
 * @author: CoreDao
 * @create: 2021-03-03 11:53
 **/

object SetTest {
  def main(args: Array[String]): Unit = {

    /**
     * 创建
     */
    val set: Set[Int] = Set(1, 2, 3)
    set.foreach(println)
    for (elem <- set) {
      println(elem)
    }

    val set1 = Set(2, 4, 5)

    /**
     * 交并差
     * 差集有先后顺序的影响
     */
    val set2 = set1.union(set)//1，2，3，4，5
    set2.foreach(println)

    val set3 = set1.diff(set)//差集，有前后关系，这里只有1，3没有4，5
    set3.foreach(println)

    val set4 = set1.intersect(set1)//2
    set4.foreach(println)

    println("-----------------")
    /**
     * 可以通过符号替代交并差
     * & = intersect
     * &~ = diff
     */
    val set5 = set & set1
    set5.foreach(println)

    val set6: Boolean = set(2)
    println(set.subsetOf(set))
    println("------------")

    //数据结构变化
//    set2.toList

    val string = set.mkString
    println(set.mkString("~"))
    println(string)

    //可变set
    //先导包
    val set7 = Set[Int](1, 2, 3, 4, 5, 6)
    set7.add(7) //只能加一个
    set7.+=(10,100,1000)
    set.foreach(println)

  }
}
